package com.manager.cms.controller;

import com.manager.cms.bo.UserRole;
import com.manager.cms.common.CommonResponse;
import com.manager.cms.common.Pageable;
import com.manager.cms.service.UserRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 
 *
 * @author 
 * @version v1.0
 * @date 2020-05-20
 */
@Slf4j
@RestController
@RequestMapping(value = "/userRole")
public class UserRoleController {

    @Autowired
    private UserRoleService userroleservice;

    @PostMapping("/save")
    public CommonResponse save(HttpServletRequest request, HttpServletResponse response, UserRole record) {
        try {
            if (record.getId()==null) {
                userroleservice.insert(record);
            } else {
                userroleservice.update(record);
            }
            return CommonResponse.succeed();
        } catch (Exception e) {
            log.error("保存失败，{}", e);
        }
        return CommonResponse.fail();
    }

    @PostMapping("/delete")
    public CommonResponse del(HttpServletRequest request, HttpServletResponse response, String id) {
        try {
            if (id == null) {
                return CommonResponse.fail("id不能为空");
            }
            userroleservice.del(id);
            return CommonResponse.succeed();
        } catch (Exception e) {
            log.error("删除失败，{}", e);
        }
        return CommonResponse.fail();
    }

    @GetMapping("/selectByPrimaryKey")
    public CommonResponse selectByPrimaryKey(HttpServletRequest request, HttpServletResponse response, String id) {
        try {
            if (id == null) {
                return CommonResponse.fail("id不能为空");
            }
            UserRole info = userroleservice.selectByPrimaryKey(id);
            return CommonResponse.succeed(info);
        } catch (Exception e) {
            log.error("查询失败，{}", e);
        }
        return CommonResponse.fail();
    }

    @GetMapping("/list")
    public CommonResponse getList(HttpServletRequest request, HttpServletResponse response, UserRole record) {
        try {
            List<UserRole> list = userroleservice.getList(record);
            return CommonResponse.succeed(list);
        } catch (Exception e) {
            log.error("查询失败，{}", e);
        }
        return CommonResponse.fail();
    }

    @GetMapping("/pageList")
    public CommonResponse pageList(HttpServletRequest request, HttpServletResponse response, UserRole record,
                                   @RequestParam(value="pageNo",defaultValue = "1")Integer pageNo,
                                   @RequestParam(value="pageSize",defaultValue = "20")Integer pageSize) {
        try {
            Pageable<UserRole> page = userroleservice.pageList(record,pageNo,pageSize);
            return CommonResponse.succeed(page);
        } catch (Exception e) {
            log.error("查询失败，{}", e);
        }
        return CommonResponse.fail();
    }
}